package org.tis.tools.abf.module.ac.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.abf.module.ac.controller.request.AcDeleteListRequest;
import org.tis.tools.abf.module.ac.dao.AcAppConfigMapper;
import org.tis.tools.abf.module.ac.entity.AcAppConfig;
import org.tis.tools.abf.module.ac.entity.AcOperatorConfig;
import org.tis.tools.abf.module.ac.entity.vo.AcAppConfigDetail;
import org.tis.tools.abf.module.ac.exception.AcExceptionCodes;
import org.tis.tools.abf.module.ac.exception.AcManagementException;
import org.tis.tools.abf.module.ac.service.IAcAppConfigService;
import org.tis.tools.abf.module.ac.service.IAcOperatorConfigService;
import org.tis.tools.abf.module.common.entity.enums.YON;
import org.tis.tools.abf.module.sys.entity.SysDict;
import org.tis.tools.abf.module.sys.entity.SysDictItem;
import org.tis.tools.abf.module.sys.service.ISysDictItemService;
import org.tis.tools.abf.module.sys.service.ISysDictService;
import org.tis.tools.core.utils.StringUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static org.tis.tools.core.utils.BasicUtil.wrap;

/**
 * acAppConfig的Service接口实现类
 *
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AcAppConfigServiceImpl extends ServiceImpl<AcAppConfigMapper, AcAppConfig> implements IAcAppConfigService {

    @Autowired
    IAcAppConfigService acAppConfigService;

    @Autowired
    private IAcOperatorConfigService acOperatorConfigService;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private ISysDictItemService sysDictItemService;


    /**
     * 新增应用个性化配置
     */
    @Override
    public AcAppConfig createRootAppConfig(String guidApp, String configType, String configName, String configDict, String configStyle, String configValue, YON enabled, BigDecimal displayOrder, String configDesc) throws AcManagementException {
        AcAppConfig acAppConfig = new AcAppConfig();

            //转换displayOrder类型
            BigDecimal displayOrderNew;
            if (null == displayOrder){
                if (null == this.baseMapper.getMaxDisplayOrder()){
                    displayOrderNew = new BigDecimal(0);
                }else {
                    displayOrderNew = new BigDecimal(this.baseMapper.getMaxDisplayOrder() + 1);
                }
            }else {
                displayOrderNew = displayOrder;
            }
            //收集参数
            acAppConfig.setGuidApp(guidApp);
            acAppConfig.setConfigType(configType);
            acAppConfig.setConfigName(configName);
            acAppConfig.setConfigDict(configDict);
            acAppConfig.setConfigStyle("默认");
            acAppConfig.setConfigValue(configValue);
            acAppConfig.setEnabled(enabled);
            acAppConfig.setDisplayOrder(displayOrderNew);
            acAppConfig.setConfigDesc(configDesc);

            acAppConfigService.insert(acAppConfig);
        return acAppConfig;
    }


    /**
     * 修改个性配置
     */
    @Override
    public AcAppConfig changeById(String guid, String guidApp, String configType, String configName, String configDict, String configStyle, String configValue, YON enabled, BigDecimal displayOrder, String configDesc) throws AcManagementException {

        AcAppConfig acAppConfig = new AcAppConfig();

            //转换displayOrder类型
            BigDecimal displayOrderNew;
            if (null == displayOrder){
                if (null == this.baseMapper.getMaxDisplayOrder()){
                    displayOrderNew = new BigDecimal(0);
                }else {
                    displayOrderNew = new BigDecimal(this.baseMapper.getMaxDisplayOrder() + 1);
                }
            }else {
                displayOrderNew = displayOrder;
            }

            //收录参数
            acAppConfig.setGuid(guid);
            acAppConfig.setGuidApp(guidApp);
            acAppConfig.setConfigType(configType);
            acAppConfig.setConfigName(configName);
            acAppConfig.setConfigDict(configDict);
            acAppConfig.setConfigStyle(configStyle);
            acAppConfig.setConfigValue(configValue);
            acAppConfig.setEnabled(enabled);
            acAppConfig.setDisplayOrder(displayOrderNew);
            acAppConfig.setConfigDesc(configDesc);

            acAppConfigService.updateById(acAppConfig);
        return acAppConfig;
    }


    @Override
    public void moveAppConfig(String id) throws AcManagementException {

            //根据个性化配置ID查询出有关 操作员个性配置的信息
            Wrapper<AcOperatorConfig> wrapper = new EntityWrapper<AcOperatorConfig>();
            wrapper.eq(AcOperatorConfig.COLUMN_GUID_CONFIG,id);
            //查询出所有数据
            List<AcOperatorConfig> list = acOperatorConfigService.selectList(wrapper);
            if (0 != list.size()){
                //删除掉相关数据
                for (AcOperatorConfig acOperatorConfig : list){
                    if (null != acOperatorConfig){
                        acOperatorConfigService.deleteOperatorConfig(acOperatorConfig.getGuid());
                    }
                }
            }

            //删除个性化配置
            deleteById(id);

    }

    /**
     * 根据应用ID查询个性化配置
     *
     * @param page
     * @param wrapper
     * @param appId
     * @return
     * @throws AcManagementException
     */
    @Override
    public Page<AcAppConfigDetail> listByAppId(Page<AcAppConfig> page, EntityWrapper<AcAppConfig> wrapper, String appId)
            throws AcManagementException {
        //2018年8月29日 16:35:56 因不能实现模糊匹配所有注释,待可以实现匹配之后可放开
        // return page.setRecords(this.baseMapper.listByAppId(page,appId,wrapper));

//        if (null == wrapper){
//            wrapper = new EntityWrapper<AcAppConfig>();
//        }

        //2018年8月29日 16:36:38 自定义实现模糊匹配
        wrapper.eq(AcAppConfig.COLUMN_GUID_APP,appId);
        wrapper.orderBy(AcAppConfig.COLUMN_DISPLAY_ORDER,true);

        Page<AcAppConfig> configPage = selectPage(page,wrapper);
        List<AcAppConfig> configList = configPage.getRecords();

        if (0 == configList.size()){
            return new Page<AcAppConfigDetail>();
        }

        Page<AcAppConfigDetail> configDetailPage = new Page<>();
        List<AcAppConfigDetail> configDetailList = new ArrayList<>();
        for (AcAppConfig acAppConfig : configList){
            if (null != acAppConfig){
                String dictName = "";
                String valueName= "";
                if (StringUtil.isNotEmpty(acAppConfig.getConfigDict())){
                    SysDict sysDict = sysDictService.selectById(acAppConfig.getConfigDict());
                    if (null != sysDict){
                        dictName = sysDict.getDictName();
                    }
                }
                if (StringUtil.isNotEmpty(acAppConfig.getConfigValue())){
                    SysDictItem sysDictItem = sysDictItemService.selectById(acAppConfig.getConfigValue());
                    if (null != sysDictItem){
                        valueName = sysDictItem.getItemName();
                    }
                }
                AcAppConfigDetail acAppConfigDetail = new AcAppConfigDetail(acAppConfig,dictName,valueName);
                configDetailList.add(acAppConfigDetail);
            }
        }

        configDetailPage.setTotal(configPage.getTotal());
        configDetailPage.setSize(configPage.getSize());
        configDetailPage.setCurrent(configPage.getCurrent());
        configDetailPage.setRecords(configDetailList);

        return configDetailPage;
    }

    /**
     * 批量删除接口
     *
     * @param deleteListRequest
     * @throws AcManagementException
     */
    @Override
    public void batchDelete(AcDeleteListRequest deleteListRequest) throws AcManagementException {

        List<String> deleteList = deleteListRequest.getDeleteList();

        if (0 == deleteList.size()){
            throw new AcManagementException(AcExceptionCodes.PARAMETER_IS_EMPTY_WHRN_BATCH_DELETE,wrap("批量删除时参数列表为空"));
        }

        for (String guid : deleteList){
            if (!StringUtil.isEmpty(guid)){
                moveAppConfig(guid);
            }
        }
    }
}

